package com.suypower.carManage.json;

import com.jarp.db.DAO;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.suypower.carManage.VO.CombVO;
import com.suypower.carManage.comm.ConstUtil;
import net.sf.json.JSONArray;

import javax.servlet.ServletException;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.Vector;

public class Get_Data_List extends ActionSupport {

    DAO dao = new DAO();
    private String method;
    private String _dc;
    private String query;
    private String name;

    public String getMethod() {
        return method;
    }

    public void setMethod(String method) {
        this.method = method;
    }

    public String get_dc() {
        return _dc;
    }

    public void set_dc(String dc) {
        _dc = dc;
    }

    public String getQuery() {
        return query;
    }

    public void setQuery(String query) {
        this.query = query;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String execute() {
        String res = "";
        System.out.println("query");
        if (method.equals("car")) {
            res = this.getCarId();
        } else if (method.equals("driver")) {
            res = this.getDriver();
        } else if (method.equals("card")) {
            res = this.getCard();
        } else if(method.equals("cbgs")){
            res=this.getCbgs();
        }
        if (res.equals("")) {
            ActionContext.getContext().put("data", "{id:'000',code:'没有数据'}");
        } else {
            ActionContext.getContext().put("data", res);
        }
        return this.SUCCESS;
    }

    /**
     * 获取驾驶员列表
     *
     * @return
     */
    public String getDriver() {
        System.out.println("getDriver");
        String res = "";
        ArrayList<CombVO> list = new ArrayList<CombVO>();
        String sql = "SELECT * FROM tb_driver where 1=1";
        if(name!=null&&!name.equals("")){
          name=name.toUpperCase();  
        }
        if(name!=null&&!name.equals("")){
            sql+=" AND( ab LIKE '%"+name+"%'";
            sql+=")";
        }
        System.out.println(sql);
        try {
            Vector v = dao.getDataSet(ConstUtil.DBNAME, sql);
            if (v != null) {
                for (Object obj : v) {
                    Hashtable hash = (Hashtable) obj;
                    CombVO comb = new CombVO();
                    comb.setName((String) hash.get("NAME"));
                    comb.setValue((String) hash.get("NAME"));
                    list.add(comb);
                }
                JSONArray json = JSONArray.fromObject(list);
                res = json.toString();
            } else {
                res = "{id:'000',code:'没有数据'}";
            }
        } catch (ServletException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return res;
    }

    /**
     * 获取车辆列表
     *
     * @return
     */
    public String getCarId() {
        String res = "";
        ArrayList<CombVO> list = new ArrayList<CombVO>();
        String sql = "SELECT * FROM tb_car";
        try {
            Vector v = dao.getDataSet(ConstUtil.DBNAME, sql);
            if (v != null) {
                for (Object obj : v) {
                    Hashtable hash = (Hashtable) obj;
                    CombVO comb = new CombVO();
                    comb.setName((String) hash.get("CODE"));
                    comb.setValue((String) hash.get("CODE"));
                    list.add(comb);
                }
                JSONArray json = JSONArray.fromObject(list);
                res = json.toString();
            } else {
                res = "{id:'000',code:'没有数据'}";
            }
        } catch (ServletException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return res;
    }

    public String getCard() {
        String res = "";
        ArrayList<CombVO> list = new ArrayList<CombVO>();
        String sql = "SELECT * FROM tb_refuel_card";
        try {
            Vector v = dao.getDataSet(ConstUtil.DBNAME, sql);
            if (v != null) {
                for (Object obj : v) {
                    Hashtable hash = (Hashtable) obj;
                    CombVO comb = new CombVO();
                    comb.setName((String) hash.get("CARD_CODE"));
                    comb.setValue((String) hash.get("CARD_CODE"));
                    list.add(comb);
                }
                JSONArray json = JSONArray.fromObject(list);
                res = json.toString();
            } else {
                res = "{id:'000',code:'没有数据'}";
            }
        } catch (ServletException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return res;
    }
    
    /**
     * 获取承保公司列表
     * @return 
     */
    public String getCbgs(){
        String res="";
        ArrayList<CombVO> list = new ArrayList<CombVO>();
        String sql = "SELECT DISTINCT t.insurer FROM tb_secure t";
        try {
            Vector v = dao.getDataSet(ConstUtil.DBNAME, sql);
            if (v != null) {
                for (Object obj : v) {
                    Hashtable hash = (Hashtable) obj;
                    CombVO comb = new CombVO();
                    comb.setName((String) hash.get("INSURER"));
                    comb.setValue((String) hash.get("INSURER"));
                    list.add(comb);
                }
                JSONArray json = JSONArray.fromObject(list);
                res = json.toString();
            } else {
                res = "{id:'000',code:'没有数据'}";
            }
        } catch (ServletException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return res;
    }
}
